package com.edusite

class AsignaturasController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        if (session.user.tipo != "admin")
    	redirect(uri:"/")
 
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [asignaturasInstanceList: Asignaturas.list(params), asignaturasInstanceTotal: Asignaturas.count()]
    }
    
        def listPublic = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [asignaturasInstanceList: Asignaturas.list(params), asignaturasInstanceTotal: Asignaturas.count()]
    }

    def create = {
        if (session.user.tipo != "admin")
    	redirect(uri:"/")
 
        def asignaturasInstance = new Asignaturas()
        asignaturasInstance.properties = params
        return [asignaturasInstance: asignaturasInstance]
    }

    def save = {
        def asignaturasInstance = new Asignaturas(params)
        asignaturasInstance.directorio = "Asignaturas/"+ params.idasignatura
        if (asignaturasInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), asignaturasInstance.id])}"
            def directo = new File ("Asignaturas/"+params.idasignatura)
            if (directo.mkdir())
              println "Creado el directorio! " + params.idasignatura  
            
            redirect(action: "show", id: asignaturasInstance.id)
        }
        else {
            render(view: "create", model: [asignaturasInstance: asignaturasInstance])
        }
    }

    def show = {
     if (session.user.tipo != "admin")
    	redirect(uri:"/")
        def asignaturasInstance = Asignaturas.get(params.id)
        if (!asignaturasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), params.id])}"
            redirect(action: "list")
        }
        else {
            [asignaturasInstance: asignaturasInstance]
        }
    }
 
 
    
 def showPublic = {
        def asignaturasInstance = Asignaturas.get(params.id)
        if (!asignaturasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), params.id])}"
            redirect(action: "list")
        }
        else {
            [asignaturasInstance: asignaturasInstance]
        }
    }

 
 
 
 
    def edit = {
        def asignaturasInstance = Asignaturas.get(params.id)
        if (!asignaturasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [asignaturasInstance: asignaturasInstance]
        }
    }

    def update = {
        def asignaturasInstance = Asignaturas.get(params.id)
        if (asignaturasInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (asignaturasInstance.version > version) {
                    
                    asignaturasInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'asignaturas.label', default: 'Asignaturas')] as Object[], "Another user has updated this Asignaturas while you were editing")
                    render(view: "edit", model: [asignaturasInstance: asignaturasInstance])
                    return
                }
            }
            asignaturasInstance.properties = params
            if (!asignaturasInstance.hasErrors() && asignaturasInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), asignaturasInstance.id])}"
                redirect(action: "show", id: asignaturasInstance.id)
            }
            else {
                render(view: "edit", model: [asignaturasInstance: asignaturasInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def asignaturasInstance = Asignaturas.get(params.id)
        if (asignaturasInstance) {
            try {
                asignaturasInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'asignaturas.label', default: 'Asignaturas'), params.id])}"
            redirect(action: "list")
        }
    }
    
     def salvarmatri = {
     println "Entro en salvar matriculas"
//    render (view:"salvarmatri")

    }
}
